package com.cc.controller;

import com.cc.entity.Products;
import com.cc.service.ProductService;
import com.cc.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author 33314
 * @version 1.0
 * @description: TODO
 * @date 2025/6/24 14:53
 */
@RestController
@RequestMapping("/pro")
public class ProductController {
    @Autowired
    private ProductService productService;
    /**
     * @description: 展示商品信息
     * @param:
     * @return: java.util.List<com.cc.entity.Products>
     * @author 33314
     * @date: 2025/6/24 15:13
     */
    @PostMapping("/getProductsList")
    List<Products> getProductsList(@RequestBody Map<String, String> params) {
        // 确保从请求体中正确获取参数
        String productName = params.get("productName");
        String typeName = params.get("typeName");

        // 调用Service层
        List<Products> products = productService.getProductsList(productName, typeName);
        return products;
    }
    /**
     * @description: 添加商品信息
     * @param: products
     * @return: int
     * @author 33314
     * @date: 2025/6/24 15:13
     */
    @RequestMapping("/insertProducts")
    public int insertProducts(@RequestBody Products products){
        System.out.println("接收到的productName: " + products.getProductName());
        return productService.insertProducts(products);
    }
    /**
     * @description: 通过  productId 获取商品信息
     * @param: productId
     * @return: com.cc.entity.Products
     * @author 33314
     * @date: 2025/6/24 15:14
     */
    @RequestMapping("/getProductsById")
    public Products getProductsById(@RequestParam Integer productId){
        return productService.getProductsById(productId);
    }
    /**
     * @description: 更新商品信息
     * @param: products
     * @return: int
     * @author 33314
     * @date: 2025/6/24 15:15
     */
    @RequestMapping("/updateProducts")
    public int updateProducts(@RequestBody Products products){
        return productService.updateProducts(products);
    }
    /**
     * @description: 通过 productId 删除商品信息
     * @param: productId
     * @return:
     * @author 33314
     * @date: 2025/6/24 15:15
     */
    @RequestMapping("/deleteProductsById")
    public int deleteProductsById( Integer productId){
        return productService.deleteProductsById(productId);
    }
    @RequestMapping("upload")
    public String upload(MultipartFile file){
        return FileUtil.upload(file);
    }
}
